{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b897a1c051ce4bdf9d783de8aed92e53",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"Deflection of a membrane.\n",
    "\n",
    "  -Laplace(w) = p  in the unit circle\n",
    "            w = 0  on the boundary\n",
    "\n",
    "The load p is a Gaussian function centered at (0, 0.6).\"\"\"\n",
    "from fenics import *\n",
    "from mshr import Circle, generate_mesh\n",
    "\n",
    "# Create mesh and define function space\n",
    "domain = Circle(Point(0, 0), 1)\n",
    "mesh = generate_mesh(domain, 64)\n",
    "V = FunctionSpace(mesh, 'P', 2)\n",
    "w_D = Constant(0)\n",
    "\n",
    "def boundary(x, on_boundary):\n",
    "    return on_boundary\n",
    "bc = DirichletBC(V, w_D, boundary)\n",
    "\n",
    "# Define load\n",
    "p = Expression('4*exp(-pow(beta, 2)*(pow(x[0], 2) + pow(x[1] - R0, 2)))', degree=1, beta=8, R0=0.6)\n",
    "\n",
    "# Define variational problem\n",
    "w = TrialFunction(V)\n",
    "v = TestFunction(V)\n",
    "a = dot(grad(w), grad(v))*dx\n",
    "L = p*v*dx\n",
    "\n",
    "# Compute solution\n",
    "w = Function(V)\n",
    "solve(a == L, w, bc)\n",
    "\n",
    "p = interpolate(p, V)\n",
    "\n",
    "# Curve plot along x = 0 comparing p and w\n",
    "import numpy as np\n",
    "tol = 0.001  # avoid hitting points outside the domain\n",
    "y = np.linspace(-1 + tol, 1 - tol, 101)\n",
    "points = [(0, y_) for y_ in y]  # 2D points\n",
    "w_line = np.array([w(point) for point in points])\n",
    "\n",
    "#######################################################################\n",
    "from vtkplotter.dolfin import plot\n",
    "from vtkplotter import Line\n",
    "\n",
    "wline = Line(y, w_line*10, c='white', lw=8)\n",
    "\n",
    "plot(w, wline) #deflection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
